// pages/coach/index/index.js
var common = require('../../../utils/common.js');
let application = getApp();
Page({
  data: {
    coaches: [],
    types: [],
    typeIndex: [],
    condition: {
      sort: 0,
      type: 0,
      skip: 0,
      take: 20
    },
    loadHidden: false,
    hasMore: false,
    tipNone: true,
    typetap: true,
    activeFilter: ['', 'header-active', '']
  },
  onLoad: function (options) {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    that.reqCoachs();
    that.reqCoachType();
  },
  onReachBottom: function () {
    var that = this;
    that.setData({
      tipNone: false,
      hasMore: false
    });
    that.data.condition.skip += that.data.condition.take;
    that.reqCoachs();
  },
  reqCoachType: function () {
    var that = this;
    common.asynPost(common.API.coach.types, {}, function (data) {
      if (data['types']) {
        var indexs = new Array(data['types']);
        indexs[0] = 'header-active';
        that.setData({
          types: data['types'],
          typeIndex: indexs
        });
      }
    });
  },
  reqCoachs: function () {  // 获取教练列表
    var that = this;
    var con = that.data['condition'];
    common.asynPost(common.API.coach.list, {
      sort: con.sort,
      type: con.type,
      longitude:application.globalData.longitude,
      latitude:application.globalData.latitude,
      skip: con.skip,
      take: con.take
    }, function (data) {
      !that.data['loadHidden'] && that.setData({ loadHidden: true });
      if (data['coaches'] && data['coaches'].length) {
        that.setData({
          tipNone: true,
          hasMore: false
        });
        that.setData({
          coaches: that.data.coaches.concat(data['coaches'])
        });
      } else {
        that.data.condition.skip -= that.data.condition.take;
        that.setData({
          tipNone: false,
          hasMore: true
        });
      }
    }, function () {
      that.setData({ loadHidden: true });
    });
  },
  coachTypeClick: function (event) {
    this.setData({
      typetap: !this.data.typetap
    });
  },
  coachFilter: function (event) {
    var filter = parseInt(event.currentTarget.dataset.filter);
    this.setData({
      coaches: [],
      loadHidden: false,
      'condition.skip':0
    });
    if (filter == 1) {
      wx.navigateTo({
        url: '../nearby/nearby'
      })
      // this.setData({
      //   activeFilter: ['header-active', '', '']
      // });
    } else if (filter == 0) {
      this.setData({
        activeFilter: ['', 'header-active', ''],
        'condition.sort':filter
      });
    } else if (filter == 2) {
      this.setData({
        activeFilter: ['', '', 'header-active'],
        'condition.sort':filter
      });
    }
    this.reqCoachs();
  },
  typeSelected: function (event) {
    var typeId = parseInt(event.currentTarget.dataset.typeid),
      index = parseInt(event.currentTarget.dataset.index);
    this.setData({
      coaches: [],
      loadHidden: false
    });
    this.data.condition.type = typeId;
    var len = this.data.typeIndex.length;
    var indexs = new Array(len);
    indexs[index] = "header-active";
    this.setData({
      typeIndex: indexs
    });
    this.data.condition.skip = 0;
    this.reqCoachs();
  },
  nowBook: function (event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({ url: '../detail/detail?id=' + id });
  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})